package io.bluerain.ldrawersample;

import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import io.bluerain.android.sdk.update.core.UpAppCompatActivity;
import io.bluerain.ldrawersample.entities.Menu;
import io.bluerain.ldrawersample.entities.SimpleMenu;

/**
 * Created by foredawn on 15-9-22.
 * 侧滑菜单的基本实现
 */
public class DrawerMenuActivity extends UpAppCompatActivity {

    private Menu[] featureMenus = new Menu[]{};
    private Menu[] finalMenus = new Menu[]{};
    private String title = "默认标题";
    private String subTitle = "默认子标题";
    private int mainIndex = 0;
    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private ActionBarDrawerToggle drawerToggle;
    private ListView listFeatureMenu;
    private ListView listFinalMenu;

    public DrawerMenuActivity() {
    }

    public DrawerMenuActivity(Menu[] featureMenus, Menu[] finalMenus, String title, String subTitle, int mainIndex) {
        this.featureMenus = featureMenus;
        this.finalMenus = finalMenus;
        this.title = title;
        this.subTitle = subTitle;
        this.mainIndex = mainIndex;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_layout);
        findViews();
        toolbar.setTitle(title);
        toolbar.setSubtitle(subTitle);
        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.mipmap.ic_launcher);
        getSupportActionBar().setHomeButtonEnabled(true); //设置返回键可用
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open, R.string.close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };
        drawerToggle.syncState();
        drawerLayout.setDrawerListener(drawerToggle);
        //设置菜单列表
        genMenu();
        setOnItemClickListener(listFeatureMenu, featureMenus);
        setOnItemClickListener(listFinalMenu, finalMenus);
    }

    private void setOnItemClickListener(ListView listView, final Menu[] menus) {

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                ItemFragment fragment = menus[position].getFrag();
                if (fragment != null) {
                    if (!fragment.isVisible()) {
                        getFragmentManager().beginTransaction()
                                .replace(R.id.content_view, fragment)
                                .commit();
                    }
                    //关闭所有抽屉
                    drawerLayout.closeDrawers();
                } else if (menus[position].getClickListener() != null) {
                    menus[position].getClickListener().onClick(DrawerMenuActivity.this);
                }
            }
        });

    }


    private void findViews() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        drawerLayout = (DrawerLayout) findViewById(R.id.dl_left);
        listFeatureMenu = (ListView) findViewById(R.id.list_menu);
        listFinalMenu = (ListView) findViewById(R.id.list_menu_final);
    }

    private void genMenu() {
        SimpleAdapter adapter = null;
        if (featureMenus != null && featureMenus.length > 0) {
            adapter = SimpleMenu.builder()
                    .setItems(featureMenus)
                    .genAdapter(this, R.layout.menu_item,
                            new int[]{R.id.menu_img, R.id.menu_text});
            listFeatureMenu.setAdapter(adapter);
            //默认点击
        }
        if (finalMenus != null && finalMenus.length > 0) {
            adapter = SimpleMenu.builder()
                    .setItems(finalMenus)
                    .genAdapter(this, R.layout.menu_item,
                            new int[]{R.id.menu_img, R.id.menu_text});
            ListView finalMenu = (ListView) findViewById(R.id.list_menu_final);
            finalMenu.setAdapter(adapter);
        }
    }
}
